package export;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import tableDancer.ListeA;
import tableDancer.ListeB;
import tableDancer.Revierinfo;
import tableDancer.TiernamenB;
import datenbank.DatabaseQueryMachine;

/**
 * Erstellt eine XML Export Liste im Format, wie in text/xml_format.txt angegeben.
 * 
 * @author ReDucTor
 *
 */
public class ExportXML {

	private static final String ANZAHL_UNFALLWILD = "anzahl_unfallwild";
	private static final String ANZAHL_FALLWILD = "anzahl_fallwild";
	private static final String ANZAHL = "anzahl";
	private static final String ENTRY = "entry";
	private static final String BEMERKUNG = "bemerkung";
	private static final String GEWICHT = "gewicht";
	private static final String DATUM = "datum";
	private static final String WILDKLASSE = "wildklasse";
	private static final String BEHOERDENADRESSE = "behoerdenadresse";
	private static final String REVIERART = "revierart";
	private static final String LEBENSMITTELUNTERNEHMER = "lebensmittelunternehmer";
	private static final String KREISLISTE = "kreisliste";
	private static final String SCHLUESSELNUMMER = "schluesselnummer";
	private static final String JAGDJAHR = "jagdjahr";
	private static final String REVIERNAME = "reviername";
	private static final String STRECKENLISTE = "streckenliste";
	private static final String LISTE_B = "listeB";
	private static final String LISTE_A = "listeA";
	private static final String REVIERINFO = "revierinfo";
	private Revierinfo ri;
	private DatabaseQueryMachine dqm;
	private Document doc;

	/**
	 * Erstellt ein JDom Document mit den aktuellen informationen aus der Datenbank. Um die Datei zu speichern, siehe {@link ExportXML#save()}.
	 * @param ri
	 * @param fileName
	 * @throws JDOMException
	 * @throws IOException
	 */
	public ExportXML(Revierinfo ri) throws JDOMException, IOException
	{
		super();
		System.setProperty("file.encoding","ISO-8859-1");
		this.ri = ri;
		this.dqm = new DatabaseQueryMachine();
		createFile();
	}



	private void createFile() throws JDOMException, IOException
	{

		Element root = new Element(STRECKENLISTE);
		Element ri_ele = new Element(REVIERINFO);
		Element listeA_ele = new Element(LISTE_A);
		Element listeB_ele = new Element(LISTE_B);

		doc = new Document(root);

		root.addContent(ri_ele);
		root.addContent(listeA_ele);
		root.addContent(listeB_ele);

		buildRI(ri_ele);
		buildListeA(listeA_ele);
		buildListeB(listeB_ele);
	}

	private void buildListeB(Element listeB_ele) {
		LinkedList<TiernamenB> tierB_list = dqm.getTiernamenB();
		for(TiernamenB tierB : tierB_list)
		{

			ListeB b = dqm.getListeB(ri, tierB.getId());
			if(b != null)
			{
				Element wildklasse_ele = new Element(WILDKLASSE);
				listeB_ele.addContent(wildklasse_ele);
				wildklasse_ele.setAttribute(new Attribute("name", tierB.getName()));
				wildklasse_ele.addContent(new Element(SCHLUESSELNUMMER));
				wildklasse_ele.addContent(new Element(ANZAHL));
				wildklasse_ele.addContent(new Element(ANZAHL_FALLWILD));
				wildklasse_ele.addContent(new Element(ANZAHL_UNFALLWILD));
				wildklasse_ele.addContent(new Element(BEMERKUNG));
				
				wildklasse_ele.getChild(SCHLUESSELNUMMER).addContent(String.valueOf(tierB.getId()));
				wildklasse_ele.getChild(ANZAHL).addContent(String.valueOf(b.getErlegt_gefangen()));
				wildklasse_ele.getChild(ANZAHL_FALLWILD).addContent(String.valueOf(b.getFallwild()));
				wildklasse_ele.getChild(ANZAHL_UNFALLWILD).addContent(String.valueOf(b.getFallwild_unfall()));
				wildklasse_ele.getChild(BEMERKUNG).addContent(b.getBemerkung());
			}
		}

	}
	private void buildListeA(Element listeA_ele) {
		LinkedList<ListeA> list = dqm.get_ll_ListeAWithID(ri);

		for(ListeA currentEntry : list)
		{
			Element currentEntryNode = new Element(ENTRY);
			listeA_ele.addContent(currentEntryNode);
			currentEntryNode.addContent(new Element(WILDKLASSE));
			currentEntryNode.addContent(new Element(DATUM));
			currentEntryNode.addContent(new Element(GEWICHT));
			currentEntryNode.addContent(new Element(BEMERKUNG));

			currentEntryNode.setAttribute(new Attribute("id", String.valueOf(currentEntry.getList_a_id())));
			currentEntryNode.getChild(WILDKLASSE).addContent(currentEntry.getTier_a_name());
			currentEntryNode.getChild(DATUM).addContent(currentEntry.getDatum());
			currentEntryNode.getChild(GEWICHT).addContent(String.valueOf(currentEntry.getGewicht()));
			currentEntryNode.getChild(BEMERKUNG).addContent(currentEntry.getBemerkung());
		}
	}

	private void buildRI(Element ri_ele) {
		ri_ele.addContent(new Element(REVIERNAME));
		ri_ele.addContent(new Element(JAGDJAHR));
		ri_ele.addContent(new Element(SCHLUESSELNUMMER));
		ri_ele.addContent(new Element(KREISLISTE));
		ri_ele.addContent(new Element(LEBENSMITTELUNTERNEHMER));
		ri_ele.addContent(new Element(REVIERART));
		ri_ele.addContent(new Element(BEHOERDENADRESSE));

		ri_ele.getChild(REVIERNAME).addContent(ri.getReviername());
		ri_ele.getChild(JAGDJAHR).addContent(String.valueOf(ri.getJahr_erhebungsstand()));
		ri_ele.getChild(SCHLUESSELNUMMER).addContent(String.valueOf(ri.getAmtliche_schluesselnr()));
		ri_ele.getChild(KREISLISTE).addContent(String.valueOf(ri.getKreisliste()));
		ri_ele.getChild(LEBENSMITTELUNTERNEHMER).addContent(String.valueOf(ri.getLebensmittelunternehmer()));
		ri_ele.getChild(REVIERART).addContent(ri.getRevierart().toString());
		ri_ele.getChild(BEHOERDENADRESSE).addContent(ri.getAdresse());
	}

	public void save(String path) throws IOException
	{
		Format format = Format.getPrettyFormat();
		format.setEncoding("iso-8859-1");
		XMLOutputter outputter = new XMLOutputter(format);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String fileName = String.format("streckenliste_%s_%s.xml", ri.getReviername(), sdf.format(new Date()));
		FileWriter writer = new FileWriter(new File(path, fileName));
		outputter.output(doc,writer);
		writer.close(); 
	}

	/*public static void main(String[] args) {
		DatabaseQueryMachine dqm = new DatabaseQueryMachine();

		try {
			ExportXML xml = new ExportXML(dqm.getAllRevierinfo().getFirst(), "");
			xml.save();
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}*/
}
